var	TimerID;
var	UpdateQueued;

function ecxup_reload_mainwin()
{
	var	mainwin = parent.main;

	mainwin.location.reload();
}

function ecxup_check_dynamics_count(count)
{
	var	mainwin = parent.main;
	var	dynamics;

	if (!mainwin)
		return;

	dynamics = mainwin.document.getElementsByName('DYNAMIC');
	if (dynamics.length != count)
		ecxup_reload_mainwin();
}

function ecxup(TYPE, DOM, attrname, attrvalue)
{
	var	mainwin, maindoc;
	var	elmt;
	
	if (parent == null)
		return;
	mainwin = parent.main;
	if (mainwin == null)
		return;
	maindoc = mainwin.document;
	if (maindoc == null)
		return;
	elmt = maindoc.getElementById(DOM);
	if (elmt == null || elmt.tagName != TYPE) {
		ecxup_reload_mainwin();
		return;
	}

	if (TYPE == 'SPAN' && attrname == 'innerHTML') {
		if (elmt.innerHTML == null) {
			ecxup_reload_mainwin();
			return;
		}
		elmt.innerHTML = attrvalue;
	}
	else if (TYPE == 'ANCHOR' && attrname == 'href') {
		if (attrvalue.length == 0)
			elmt.removeAttribute('href');
		else {
			if (elmt.href == null) {
				ecxup_reload_mainwin();
				return;
			}
			elmt.href = attrvalue;
		}
	}
	else
		elmt.setAttribute(attrname, attrvalue, 0);
}

function ReloadECXUpdater(ecx_page_id)
{
	var	URL = "/ecxup?ecx_page_id=" + ecx_page_id;

	window.location.replace(URL);
}

function getSessionId(doc)
{
	var	reg = new RegExp('SessionId=[_a-zA-Z0-9]+($|;)', 'g');
	var	sessId;

	sessId = doc.cookie.match(reg);
	if (!sessId || !sessId[0])
		return null;
	return sessId[0].substring(10, sessId[0].length).replace(';', '');
}

function SetDocumentCookie(ecx_doc)
{
	var	SessionId = getSessionId(ecx_doc);
	var	id_hidden = ecx_doc.getElementById('ECX_PAGE_ID');
	var	ecx_page_id;

	if (id_hidden) {
		ecx_page_id = id_hidden.value;
	}

	document.cookie = "SessionId=" + SessionId;
	document.cookie = "ecx_page_id=" + ecx_page_id;
}

function KickUpdateTimer(ecx_page_id, timeout)
{
	var	code;

	code = "ReloadECXUpdater(" + ecx_page_id + ")";
	TimerID = window.setTimeout(code, timeout);
	UpdateQueued = true;
}

function RegDocument(ecx_doc, timeout)
{
	var	id_hidden;

	if (UpdateQueued) {
		clearTimeout(TimerID);
		UpdateQueued = false;
	}

	id_hidden = ecx_doc.getElementById('ECX_PAGE_ID');
	if (!id_hidden)
		return;
	KickUpdateTimer(id_hidden.value, timeout);
}

function UnregDocument(ecx_doc)
{
	if (UpdateQueued) {
		clearTimeout(TimerID);
		UpdateQueued = false;
	}
}
